#!/usr/bin/env python

#import sqlite argparser
import sqlite3, argparse


from time import strftime, localtime, gmtime
#taken from http://www.uptill3.com/2010/04/20/iphone-forensics-sans-iphone/
sms_flags = {
    2 : 'Recd SMS',
    3 : 'Sent SMS',
    4 : 'Recd MMS',
    33 : 'Unsent' ,
    35 : 'Failed Send',
    129 : 'Deleted'}
read_flags = {
    0 : 'Unread',
    1: 'Read' }


def run(s):
	#check if the file sms.db is there
	try:
		print('Found "{}"'.format(s))
		#connect to the db

		conn = sqlite3.connect(s)
		c = conn.cursor()
		#print the column headers
		print()
		#get the table to print
		for ROWID, address, date, text, flags, read in c.execute('select ROWID, address, date, text, flags, read from message'):
			#convert the time to the usual format
			time = strftime('%Y-%m-%d %H:%M:%S (%Z)', localtime(date))
			
			#convert the read using the dict defined above
			readVal = read_flags.get(read)

			#convert the flag of the sms using the dict defined above
			flagVal = sms_flags.get(flags)	

			#convert the row id into string

			row = str(ROWID)

			#prints the values
			print('{0:10}{1:15}{2:30}{3:20}{4:20}{5:10}'.format(row, address, time, text, flagVal, readVal))

		#endofloop
			
	except sqlite3.Error:
		print('The sms.db is not placed in the current working directory')    

	

def main():
	#execute the operation on the sms.db file
	
	databaseName = 'sms.db'
	run(databaseName)

#exec main
if __name__ == '__main__':
	main()